package com.tedu.bill.demo7.demo6;

import java.util.Random;

/**
 * Author：毕龙
 * Date: 2024/5/25 15:41
 */
public class MyThread1 extends Thread {
    //共享的数据
    static double money = 100;
    static int count = 3;

    //最小的奖金金额是0.01
    static final double MIN_MONEY = 0.01;

    @Override
    public void run() {
        synchronized (Thread.class){
            if(count == 0){
                System.out.println(getName()+"没有抢到红包");
            }else{
                //定义一个
                double prize = 0;

                //判断红包是否到达末尾
                if(count == 1){
                    prize = money;

                }       else{
                    //表示随机一个红包

                    //随机红包的范围  moeny - (count-1)*min
                    double bounds = money-(count-1)*MIN_MONEY;
                    prize = Math.random()*bounds;
                    if(prize<MIN_MONEY){
                        prize = MIN_MONEY;
                    }
                }
                money = money-prize;
                count--;
                String str = String.format("%.2f", prize);
                System.out.println(getName()+"抢到了"+str+"元");

            }
        }
    }
}
